<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="user-search"
      align="left"
    >
      <el-button type="primary" @click="addOne()">新增</el-button>
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.style_name"
          placeholder="输入仓库名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search()"
          >搜索</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="danger" @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="style_code"
        label="仓库分类编码"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="style_name"
        label="仓库分类名称"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="update(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteOne(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer" align="center">
      <el-button size="small" @click="cancel()">取消</el-button>
      <el-button size="small" type="primary" class="title" @click="send()"
        >保存</el-button
      >
    </div>
    <Page
      v-bind:child-msg="pageparm"
      @callFather="callFather"
      align="center"
    ></Page>
    <el-dialog
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
      append-to-body
    >
      <div line-height="50px">
        <el-form
          label-position="left"
          label-width="120px"
          :model="editForm"
          :rules="rules"
          ref="editForm"
        >
          <el-form-item
            label="仓库分类编码"
            prop="style_code"
            style="position: relative;bottom: 15px;"
          >
            <el-input
              size="small"
              v-model="editForm.style_code"
              auto-complete="off"
              placeholder="请输入仓库分类编码"
            ></el-input>
          </el-form-item>

          <el-form-item
            label="仓库分类名称"
            prop="style_name"
            style="position: relative;top: 5px;"
          >
            <el-input
              size="small"
              v-model="editForm.style_name"
              auto-complete="off"
              placeholder="请输入仓库分类名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" @click="save()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Page from "@/components/Pagination";
import axios from "axios";
export default {
  data() {
    //校验器
    var checkCode = (rules, value, callback) => {
      this.$axios.get("/api/warehousestyle/findByCode/" + value).then(Response => {
        if (Response.data.code == 500) {
          callback();
        }
        callback(new Error("编码已存在！"));
      });
    };
    var checkName = (rules, value, callback) => {
      this.$axios
        .get("/api/warehousestyle/findByUname/" + encodeURI(encodeURI(value)))
        .then(Response => {
          if (Response.data.code == 500) {
            callback();
          }
          callback(new Error("名称已存在！"));
        });
    };
    return {
      editFormVisible: false,
      batchDeleteArr: [],
      editForm: {
        id: "",
        style_code: "",
        style_name: ""
      },
      // rules表单验证
      rules: {
        style_code: [
          { required: true, message: "请输入仓库分类编码", trigger: "blur" },
           {
            pattern: /^[A-Za-z0-9]{4}$/,
            message: "请输入4位数字、字母",
            trigger: "blur"
          },
          { validator: checkCode, trigger: "blur" }
        ],
        style_name: [
          { required: true, message: "请输入仓库分类名称", trigger: "blur" },
          { min: 1, max: 20, message: "请输入1~20个字符", trigger: "blur" },
          { validator: checkName, trigger: "blur" }
        ]
      },
      formInline: {
        page: 1,
        limit: 10,
        style_code: "",
        style_name: ""
      },
      tableData: [],
      //分页信息
      pageparm: {
        currentPage: 1,
        pageSize: 5,
        total: 0
      }
    };
  },
  components: {
    Page
  },
  created() {
    this.findAllManager();
  },
  methods: {
    findAllManager() {
      this.$axios
        .get(
          "/api/warehousestyle/findAll/" +
            this.pageparm.currentPage +
            "/" +
            this.pageparm.pageSize
        )
        .then(res => {
          console.log(res);
          if (res.data.code == 200) {
            this.tableData = res.data.data.list;
            this.pageparm.total = res.data.data.total;
          }
        })
        .catch();
    },
    // 分页插件事件
    callFather(parm) {
      this.pageparm.currentPage = parm.currentPage;
      this.pageparm.pageSize = parm.pageSize;
      this.findAllManager();
    },
    //改变分页大小
    changeSize(val) {
      this.pageparm.pageSize = val;
      this.pageparm.currentPage = 1;
      // 重新调用获取数据方法
      this.findAllManager();
    },

    //改变当前页
    changeCurrent(val) {
      this.pageparm.currentPage = val;
      // 重新调用获取数据方法
      this.findAllManager();
    },
    handleSelectionChange(selection) {
      this.selectNum = selection.length;
      this.selection = selection;
      console.log(this.selectNum);
    },
    // 添加一条仓库分类
    addOne() {
      this.editFormVisible = true;
      this.title = "添加分类";
      this.editForm.id = "";
      this.editForm.style_code = "";
      this.editForm.style_name = "";
    },
    update(row) {
      this.editFormVisible = true;
      this.title = "修改分类";
      this.editForm.id = row.id;
      this.editForm.style_code = row.style_code;
      this.editForm.style_name = row.style_name;
    },
    //传值给父组件
    send() {
      if (this.selectNum == 0) {
        this.$emit("sendClassify", "");
      } else if (this.selection == undefined) {
        this.$emit("sendClassify", "");
      } else {
        this.$emit("sendClassify", this.selection[0].style_name);
      }
      this.findAllManager();
      console.log(this.selection);
    },

    //取消传值给父组件
    cancel() {
      this.$emit("cancelClassify", "");
      this.selectNum == 0;
      this.findAllManager();
    },
    //保存添加的仓库分类

    save() {
      this.$refs["editForm"].validate(valid => {
        if (!valid) {
          this.$message.error("请按要求填写表单！");
        } else {
          if (this.editForm.id == "") {
            this.$axios
              .post("/api/warehousestyle/addOne", this.editForm)
              .then(res => {
                console.log(res);
                if (res.data.code == 200) {
                  this.$message({
                    message: "添加成功",
                    type: "success"
                  });
                  //成功后关掉弹出框
                  this.editFormVisible = false;
                  //同时刷新数据
                  this.findAllManager();
                } else {
                  this.$message({
                    type: "info",
                    message: res.data.message
                  });
                }
              })
              .catch(err => {
                this.editFormVisible = false;
                this.$message.error("保存失败，请稍后再试!");
              });
          } else {
            this.$axios
              .put("/api/warehousestyle/update", this.editForm)
              .then(res => {
                console.log(res);
                if (res.data.code == 200) {
                  this.$message({
                    message: "修改成功",
                    type: "success"
                  });
                  this.editFormVisible = false;
                  this.findAllManager();
                }else{
                  this.$message({
                    type: "info",
                    message: res.data.message
                  });
                }
              })
              .catch(err => {
                this.editFormVisible = false;
                this.$message.error("保存失败，请稍后再试!");
              });
          }
        }
      });
    },
    //删除一条仓库分类
    deleteOne(row) {
      this.$confirm("确定要删除吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .put("/api/warehousestyle/deleteOne/" + row.style_code)
            .then(res => {
              console.log(res);
              if (res.data.code == 200) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                });
                this.findAllManager();
              } else {
                this.$message({
                  type: "info",
                  message: res.data.message
                });
              }
            })
            .catch(err => {
              this.$message.error("删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 搜索事件
    search() {
      if (!(this.formInline.style_name == "")) {
        this.$axios
          .get(
            "/api/warehousestyle/findByName/" +
              this.pageparm.currentPage +
              "/" +
              this.pageparm.pageSize +
              "/" +
              encodeURI(encodeURI(this.formInline.style_name))
          )
          .then(res => {
            console.log(res);
            if (res.data.code == 200) {
              this.tableData = res.data.data.list;
              this.pageparm.total = res.data.data.total;
            }
          })
          .catch();
      } else {
        this.findAllManager();
      }
    },
    //重置数据列表
    reset() {
      this.findAllManager();
      this.formInline.style_name = "";
    },
    //批量删除数据
    deleteSome() {
      this.$confirm("确定要删除吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .put("/api/warehousestyle/deleteSome", this.batchDeleteArr)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  message: "批量删除成功",
                  type: "success"
                });
                this.findAllManager();
              } else {
                this.$message({
                  type: "info",
                  message: res.data.message
                });
              }
            })
            .catch(err => {
              this.$message.error("删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    closeDialog() {
      this.editFormVisible = false;
    }
  }
};
</script>
